{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Use case proposed in [issue #78](https://github.com/bsorrentino/langgraph4j/issues/93) by [tansice](https://github.com/tansice)\n",
    "\n",
    "## There is a null pointer issue in ToolExecutionRequestSerializer."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [],
   "source": [
    "var userHomeDir = System.getProperty(\"user.home\");\n",
    "var localRespoUrl = \"file://\" + userHomeDir + \"/.m2/repository/\";\n",
    "var langchain4jVersion = \"1.0.1\";\n",
    "var langchain4jbeta = \"1.0.1-beta6\";\n",
    "var langgraph4jVersion = \"1.6-SNAPSHOT\";"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [],
   "source": [
    "%%bash \n",
    "rm -rf \\{userHomeDir}/Library/Jupyter/kernels/rapaio-jupyter-kernel/mima_cache/org/bsc/langgraph4j"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "%dependency /add-repo local \\{localRespoUrl} release|never snapshot|always\n",
    "// %dependency /list-repos\n",
    "%dependency /add org.slf4j:slf4j-jdk14:2.0.9\n",
    "%dependency /add org.bsc.langgraph4j:langgraph4j-core:\\{langgraph4jVersion}\n",
    "%dependency /add org.bsc.langgraph4j:langgraph4j-langchain4j:\\{langgraph4jVersion}\n",
    "%dependency /add org.bsc.langgraph4j:langgraph4j-agent-executor:\\{langgraph4jVersion}\n",
    "%dependency /add dev.langchain4j:langchain4j:\\{langchain4jVersion}\n",
    "%dependency /add dev.langchain4j:langchain4j-ollama:\\{langchain4jbeta}\n",
    "\n",
    "%dependency /resolve"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "**Initialize Logger**"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [],
   "source": [
    "try( var file = new java.io.FileInputStream(\"./logging.properties\")) {\n",
    "    java.util.logging.LogManager.getLogManager().readConfiguration( file );\n",
    "}\n",
    "\n",
    "var log = org.slf4j.LoggerFactory.getLogger(\"default\");\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [],
   "source": [
    "import dev.langchain4j.agent.tool.P;\n",
    "import dev.langchain4j.agent.tool.Tool;\n",
    "import dev.langchain4j.data.message.AiMessage;\n",
    "import dev.langchain4j.data.message.ChatMessage;\n",
    "import dev.langchain4j.data.message.UserMessage;\n",
    "import dev.langchain4j.model.chat.request.ChatRequest;\n",
    "import dev.langchain4j.model.chat.request.ChatRequestParameters;\n",
    "import dev.langchain4j.model.ollama.OllamaStreamingChatModel;\n",
    "import org.bsc.langgraph4j.action.AsyncEdgeAction;\n",
    "import org.bsc.langgraph4j.action.AsyncNodeAction;\n",
    "import org.bsc.langgraph4j.action.EdgeAction;\n",
    "import org.bsc.langgraph4j.action.NodeAction;\n",
    "import org.bsc.langgraph4j.langchain4j.generators.StreamingChatGenerator;\n",
    "import org.bsc.langgraph4j.langchain4j.serializer.std.ChatMesssageSerializer;\n",
    "import org.bsc.langgraph4j.langchain4j.serializer.std.ToolExecutionRequestSerializer;\n",
    "import org.bsc.langgraph4j.langchain4j.tool.ToolNode;\n",
    "import org.bsc.langgraph4j.prebuilt.MessagesState;\n",
    "import org.bsc.langgraph4j.prebuilt.MessagesStateGraph;\n",
    "import org.bsc.langgraph4j.serializer.std.ObjectStreamStateSerializer;\n",
    "import org.slf4j.Logger;\n",
    "import org.slf4j.LoggerFactory;\n",
    "\n",
    "import java.util.Map;\n",
    "\n",
    "import static org.bsc.langgraph4j.StateGraph.END;\n",
    "import static org.bsc.langgraph4j.StateGraph.START;\n",
    "import static org.bsc.langgraph4j.action.AsyncEdgeAction.edge_async;\n",
    "import static org.bsc.langgraph4j.action.AsyncNodeAction.node_async;\n",
    "\n",
    "static class SearchTool {\n",
    "    @Tool(\"Use to surf the web, fetch current information, check the weather, and retrieve other information.\")\n",
    "    String execQuery(@P(\"The query to use in your search.\") String query) {\n",
    "        return \"Cold, with a low of 13 degrees\";\n",
    "    }\n",
    "}\n",
    "\n",
    "void testToolsStreamingChat( String modelName ) throws Exception {\n",
    "    // Setup streaming model\n",
    "    var model = OllamaStreamingChatModel.builder()\n",
    "            .baseUrl(\"http://localhost:11434\")\n",
    "            .temperature(0.5)\n",
    "            .logRequests(true)\n",
    "            .logResponses(true)\n",
    "            .modelName( modelName )\n",
    "            //.modelName(\"qwen2.5:7b\")\n",
    "            //.modelName(\"llama3.1:latest\")\n",
    "            .build();\n",
    "\n",
    "    // Setup tools\n",
    "    var tools = ToolNode.builder()\n",
    "            .specification(new SearchTool())\n",
    "            .build();\n",
    "\n",
    "    // Setup serializers\n",
    "    ObjectStreamStateSerializer<MessagesState<ChatMessage>> stateSerializer = new ObjectStreamStateSerializer<>(MessagesState::new);\n",
    "    stateSerializer.mapper()\n",
    "            .register(dev.langchain4j.agent.tool.ToolExecutionRequest.class, new ToolExecutionRequestSerializer())\n",
    "            .register(ChatMessage.class, new ChatMesssageSerializer());\n",
    "\n",
    "    // Define graph\n",
    "    NodeAction<MessagesState<ChatMessage>> callModel = state -> {\n",
    "        var generator = StreamingChatGenerator.<MessagesState<ChatMessage>>builder()\n",
    "                .mapResult(response -> Map.of(\"messages\", response.aiMessage()))\n",
    "                .startingNode(\"agent\")\n",
    "                .startingState(state)\n",
    "                .build();\n",
    "\n",
    "        var parameters = ChatRequestParameters.builder()\n",
    "                .toolSpecifications(tools.toolSpecifications())\n",
    "                .build();\n",
    "        var request = ChatRequest.builder()\n",
    "                .messages(state.messages())\n",
    "                .parameters(parameters)\n",
    "                .build();\n",
    "\n",
    "        model.chat(request, generator.handler());\n",
    "\n",
    "        return Map.of(\"_streaming_messages\", generator);\n",
    "    };\n",
    "\n",
    "    EdgeAction<MessagesState<ChatMessage>> routeMessage = state -> {\n",
    "        var lastMessage = state.lastMessage()\n",
    "                .orElseThrow(() -> new IllegalStateException(\"last message not found!\"));\n",
    "\n",
    "        if (lastMessage instanceof AiMessage message) {\n",
    "            if (message.hasToolExecutionRequests()) {\n",
    "                return \"next\";\n",
    "            }\n",
    "        }\n",
    "\n",
    "        return \"exit\";\n",
    "    };\n",
    "\n",
    "    NodeAction<MessagesState<ChatMessage>> invokeTool = state -> {\n",
    "        var lastMessage = state.lastMessage()\n",
    "                .orElseThrow(() -> new IllegalStateException(\"last message not found!\"));\n",
    "\n",
    "        if (lastMessage instanceof AiMessage lastAiMessage) {\n",
    "            var result = tools.execute(lastAiMessage.toolExecutionRequests(), null)\n",
    "                    .orElseThrow(() -> new IllegalStateException(\"no tool found!\"));\n",
    "\n",
    "            return Map.of(\"messages\", result);\n",
    "        }\n",
    "\n",
    "        throw new IllegalStateException(\"invalid last message\");\n",
    "    };\n",
    "\n",
    "    var workflow = new MessagesStateGraph<>(stateSerializer)\n",
    "            .addNode(\"agent\", node_async(callModel))\n",
    "            .addNode(\"tools\", node_async(invokeTool))\n",
    "            .addEdge(START, \"agent\")\n",
    "            .addConditionalEdges(\"agent\",\n",
    "                    edge_async(routeMessage),\n",
    "                    Map.of(\"next\", \"tools\", \"exit\", END))\n",
    "            .addEdge(\"tools\", \"agent\");\n",
    "\n",
    "    var app = workflow.compile();\n",
    "\n",
    "    var output = app.stream(Map.of(\"messages\", UserMessage.from(\"How is the weather in New York today?\")));\n",
    "    for (var out : output) {\n",
    "        log.info(\"StreamingOutput: {}\", out);\n",
    "    }\n",
    "}\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "START \n",
      "ToolExecutionRequest id is null! \n",
      "StreamingOutput: NodeOutput{node=__START__, state={messages=[UserMessage { name = null contents = [TextContent { text = \"How is the weather in New York today?\" }] }]}} \n",
      "ToolExecutionRequest id is null! \n",
      "execute: execQuery \n",
      "ToolExecutionRequest id is null! \n",
      "ToolExecutionResultMessage id is null! \n",
      "StreamingOutput: NodeOutput{node=agent, state={messages=[UserMessage { name = null contents = [TextContent { text = \"How is the weather in New York today?\" }] }, AiMessage { text = null toolExecutionRequests = [ToolExecutionRequest { id = null, name = \"execQuery\", arguments = \"{\n",
      "  \"query\" : \"weather in New York today\"\n",
      "}\" }] }]}} \n",
      "ToolExecutionRequest id is null! \n",
      "ToolExecutionResultMessage id is null! \n",
      "StreamingOutput: NodeOutput{node=tools, state={messages=[UserMessage { name = null contents = [TextContent { text = \"How is the weather in New York today?\" }] }, AiMessage { text = null toolExecutionRequests = [ToolExecutionRequest { id = null, name = \"execQuery\", arguments = \"{\n",
      "  \"query\" : \"weather in New York today\"\n",
      "}\" }] }, ToolExecutionResultMessage { id = null toolName = \"execQuery\" text = \"Cold, with a low of 13 degrees\" }]}} \n",
      "StreamingOutput: StreamingOutput{node=agent, state={messages=[UserMessage { name = null contents = [TextContent { text = \"How is the weather in New York today?\" }] }, AiMessage { text = null toolExecutionRequests = [ToolExecutionRequest { id = null, name = \"execQuery\", arguments = \"{\n",
      "  \"query\" : \"weather in New York today\"\n",
      "}\" }] }, ToolExecutionResultMessage { id = null toolName = \"execQuery\" text = \"Cold, with a low of 13 degrees\" }]}, chunk=The} \n",
      "StreamingOutput: StreamingOutput{node=agent, state={messages=[UserMessage { name = null contents = [TextContent { text = \"How is the weather in New York today?\" }] }, AiMessage { text = null toolExecutionRequests = [ToolExecutionRequest { id = null, name = \"execQuery\", arguments = \"{\n",
      "  \"query\" : \"weather in New York today\"\n",
      "}\" }] }, ToolExecutionResultMessage { id = null toolName = \"execQuery\" text = \"Cold, with a low of 13 degrees\" }]}, chunk= current} \n",
      "StreamingOutput: StreamingOutput{node=agent, state={messages=[UserMessage { name = null contents = [TextContent { text = \"How is the weather in New York today?\" }] }, AiMessage { text = null toolExecutionRequests = [ToolExecutionRequest { id = null, name = \"execQuery\", arguments = \"{\n",
      "  \"query\" : \"weather in New York today\"\n",
      "}\" }] }, ToolExecutionResultMessage { id = null toolName = \"execQuery\" text = \"Cold, with a low of 13 degrees\" }]}, chunk= weather} \n",
      "StreamingOutput: StreamingOutput{node=agent, state={messages=[UserMessage { name = null contents = [TextContent { text = \"How is the weather in New York today?\" }] }, AiMessage { text = null toolExecutionRequests = [ToolExecutionRequest { id = null, name = \"execQuery\", arguments = \"{\n",
      "  \"query\" : \"weather in New York today\"\n",
      "}\" }] }, ToolExecutionResultMessage { id = null toolName = \"execQuery\" text = \"Cold, with a low of 13 degrees\" }]}, chunk= in} \n",
      "StreamingOutput: StreamingOutput{node=agent, state={messages=[UserMessage { name = null contents = [TextContent { text = \"How is the weather in New York today?\" }] }, AiMessage { text = null toolExecutionRequests = [ToolExecutionRequest { id = null, name = \"execQuery\", arguments = \"{\n",
      "  \"query\" : \"weather in New York today\"\n",
      "}\" }] }, ToolExecutionResultMessage { id = null toolName = \"execQuery\" text = \"Cold, with a low of 13 degrees\" }]}, chunk= New} \n",
      "StreamingOutput: StreamingOutput{node=agent, state={messages=[UserMessage { name = null contents = [TextContent { text = \"How is the weather in New York today?\" }] }, AiMessage { text = null toolExecutionRequests = [ToolExecutionRequest { id = null, name = \"execQuery\", arguments = \"{\n",
      "  \"query\" : \"weather in New York today\"\n",
      "}\" }] }, ToolExecutionResultMessage { id = null toolName = \"execQuery\" text = \"Cold, with a low of 13 degrees\" }]}, chunk= York} \n",
      "StreamingOutput: StreamingOutput{node=agent, state={messages=[UserMessage { name = null contents = [TextContent { text = \"How is the weather in New York today?\" }] }, AiMessage { text = null toolExecutionRequests = [ToolExecutionRequest { id = null, name = \"execQuery\", arguments = \"{\n",
      "  \"query\" : \"weather in New York today\"\n",
      "}\" }] }, ToolExecutionResultMessage { id = null toolName = \"execQuery\" text = \"Cold, with a low of 13 degrees\" }]}, chunk= is} \n",
      "StreamingOutput: StreamingOutput{node=agent, state={messages=[UserMessage { name = null contents = [TextContent { text = \"How is the weather in New York today?\" }] }, AiMessage { text = null toolExecutionRequests = [ToolExecutionRequest { id = null, name = \"execQuery\", arguments = \"{\n",
      "  \"query\" : \"weather in New York today\"\n",
      "}\" }] }, ToolExecutionResultMessage { id = null toolName = \"execQuery\" text = \"Cold, with a low of 13 degrees\" }]}, chunk= cold} \n",
      "StreamingOutput: StreamingOutput{node=agent, state={messages=[UserMessage { name = null contents = [TextContent { text = \"How is the weather in New York today?\" }] }, AiMessage { text = null toolExecutionRequests = [ToolExecutionRequest { id = null, name = \"execQuery\", arguments = \"{\n",
      "  \"query\" : \"weather in New York today\"\n",
      "}\" }] }, ToolExecutionResultMessage { id = null toolName = \"execQuery\" text = \"Cold, with a low of 13 degrees\" }]}, chunk=,} \n",
      "StreamingOutput: StreamingOutput{node=agent, state={messages=[UserMessage { name = null contents = [TextContent { text = \"How is the weather in New York today?\" }] }, AiMessage { text = null toolExecutionRequests = [ToolExecutionRequest { id = null, name = \"execQuery\", arguments = \"{\n",
      "  \"query\" : \"weather in New York today\"\n",
      "}\" }] }, ToolExecutionResultMessage { id = null toolName = \"execQuery\" text = \"Cold, with a low of 13 degrees\" }]}, chunk= with} \n",
      "StreamingOutput: StreamingOutput{node=agent, state={messages=[UserMessage { name = null contents = [TextContent { text = \"How is the weather in New York today?\" }] }, AiMessage { text = null toolExecutionRequests = [ToolExecutionRequest { id = null, name = \"execQuery\", arguments = \"{\n",
      "  \"query\" : \"weather in New York today\"\n",
      "}\" }] }, ToolExecutionResultMessage { id = null toolName = \"execQuery\" text = \"Cold, with a low of 13 degrees\" }]}, chunk= the} \n",
      "StreamingOutput: StreamingOutput{node=agent, state={messages=[UserMessage { name = null contents = [TextContent { text = \"How is the weather in New York today?\" }] }, AiMessage { text = null toolExecutionRequests = [ToolExecutionRequest { id = null, name = \"execQuery\", arguments = \"{\n",
      "  \"query\" : \"weather in New York today\"\n",
      "}\" }] }, ToolExecutionResultMessage { id = null toolName = \"execQuery\" text = \"Cold, with a low of 13 degrees\" }]}, chunk= temperature} \n",
      "StreamingOutput: StreamingOutput{node=agent, state={messages=[UserMessage { name = null contents = [TextContent { text = \"How is the weather in New York today?\" }] }, AiMessage { text = null toolExecutionRequests = [ToolExecutionRequest { id = null, name = \"execQuery\", arguments = \"{\n",
      "  \"query\" : \"weather in New York today\"\n",
      "}\" }] }, ToolExecutionResultMessage { id = null toolName = \"execQuery\" text = \"Cold, with a low of 13 degrees\" }]}, chunk= expected} \n",
      "StreamingOutput: StreamingOutput{node=agent, state={messages=[UserMessage { name = null contents = [TextContent { text = \"How is the weather in New York today?\" }] }, AiMessage { text = null toolExecutionRequests = [ToolExecutionRequest { id = null, name = \"execQuery\", arguments = \"{\n",
      "  \"query\" : \"weather in New York today\"\n",
      "}\" }] }, ToolExecutionResultMessage { id = null toolName = \"execQuery\" text = \"Cold, with a low of 13 degrees\" }]}, chunk= to} \n",
      "StreamingOutput: StreamingOutput{node=agent, state={messages=[UserMessage { name = null contents = [TextContent { text = \"How is the weather in New York today?\" }] }, AiMessage { text = null toolExecutionRequests = [ToolExecutionRequest { id = null, name = \"execQuery\", arguments = \"{\n",
      "  \"query\" : \"weather in New York today\"\n",
      "}\" }] }, ToolExecutionResultMessage { id = null toolName = \"execQuery\" text = \"Cold, with a low of 13 degrees\" }]}, chunk= drop} \n",
      "StreamingOutput: StreamingOutput{node=agent, state={messages=[UserMessage { name = null contents = [TextContent { text = \"How is the weather in New York today?\" }] }, AiMessage { text = null toolExecutionRequests = [ToolExecutionRequest { id = null, name = \"execQuery\", arguments = \"{\n",
      "  \"query\" : \"weather in New York today\"\n",
      "}\" }] }, ToolExecutionResultMessage { id = null toolName = \"execQuery\" text = \"Cold, with a low of 13 degrees\" }]}, chunk= to} \n",
      "StreamingOutput: StreamingOutput{node=agent, state={messages=[UserMessage { name = null contents = [TextContent { text = \"How is the weather in New York today?\" }] }, AiMessage { text = null toolExecutionRequests = [ToolExecutionRequest { id = null, name = \"execQuery\", arguments = \"{\n",
      "  \"query\" : \"weather in New York today\"\n",
      "}\" }] }, ToolExecutionResultMessage { id = null toolName = \"execQuery\" text = \"Cold, with a low of 13 degrees\" }]}, chunk= } \n",
      "StreamingOutput: StreamingOutput{node=agent, state={messages=[UserMessage { name = null contents = [TextContent { text = \"How is the weather in New York today?\" }] }, AiMessage { text = null toolExecutionRequests = [ToolExecutionRequest { id = null, name = \"execQuery\", arguments = \"{\n",
      "  \"query\" : \"weather in New York today\"\n",
      "}\" }] }, ToolExecutionResultMessage { id = null toolName = \"execQuery\" text = \"Cold, with a low of 13 degrees\" }]}, chunk=1} \n",
      "StreamingOutput: StreamingOutput{node=agent, state={messages=[UserMessage { name = null contents = [TextContent { text = \"How is the weather in New York today?\" }] }, AiMessage { text = null toolExecutionRequests = [ToolExecutionRequest { id = null, name = \"execQuery\", arguments = \"{\n",
      "  \"query\" : \"weather in New York today\"\n",
      "}\" }] }, ToolExecutionResultMessage { id = null toolName = \"execQuery\" text = \"Cold, with a low of 13 degrees\" }]}, chunk=3} \n",
      "StreamingOutput: StreamingOutput{node=agent, state={messages=[UserMessage { name = null contents = [TextContent { text = \"How is the weather in New York today?\" }] }, AiMessage { text = null toolExecutionRequests = [ToolExecutionRequest { id = null, name = \"execQuery\", arguments = \"{\n",
      "  \"query\" : \"weather in New York today\"\n",
      "}\" }] }, ToolExecutionResultMessage { id = null toolName = \"execQuery\" text = \"Cold, with a low of 13 degrees\" }]}, chunk= degrees} \n",
      "StreamingOutput: StreamingOutput{node=agent, state={messages=[UserMessage { name = null contents = [TextContent { text = \"How is the weather in New York today?\" }] }, AiMessage { text = null toolExecutionRequests = [ToolExecutionRequest { id = null, name = \"execQuery\", arguments = \"{\n",
      "  \"query\" : \"weather in New York today\"\n",
      "}\" }] }, ToolExecutionResultMessage { id = null toolName = \"execQuery\" text = \"Cold, with a low of 13 degrees\" }]}, chunk=.} \n",
      "StreamingOutput: StreamingOutput{node=agent, state={messages=[UserMessage { name = null contents = [TextContent { text = \"How is the weather in New York today?\" }] }, AiMessage { text = null toolExecutionRequests = [ToolExecutionRequest { id = null, name = \"execQuery\", arguments = \"{\n",
      "  \"query\" : \"weather in New York today\"\n",
      "}\" }] }, ToolExecutionResultMessage { id = null toolName = \"execQuery\" text = \"Cold, with a low of 13 degrees\" }]}, chunk= Please} \n",
      "StreamingOutput: StreamingOutput{node=agent, state={messages=[UserMessage { name = null contents = [TextContent { text = \"How is the weather in New York today?\" }] }, AiMessage { text = null toolExecutionRequests = [ToolExecutionRequest { id = null, name = \"execQuery\", arguments = \"{\n",
      "  \"query\" : \"weather in New York today\"\n",
      "}\" }] }, ToolExecutionResultMessage { id = null toolName = \"execQuery\" text = \"Cold, with a low of 13 degrees\" }]}, chunk= dress} \n",
      "StreamingOutput: StreamingOutput{node=agent, state={messages=[UserMessage { name = null contents = [TextContent { text = \"How is the weather in New York today?\" }] }, AiMessage { text = null toolExecutionRequests = [ToolExecutionRequest { id = null, name = \"execQuery\", arguments = \"{\n",
      "  \"query\" : \"weather in New York today\"\n",
      "}\" }] }, ToolExecutionResultMessage { id = null toolName = \"execQuery\" text = \"Cold, with a low of 13 degrees\" }]}, chunk= accordingly} \n",
      "ToolExecutionRequest id is null! \n",
      "ToolExecutionResultMessage id is null! \n",
      "StreamingOutput: StreamingOutput{node=agent, state={messages=[UserMessage { name = null contents = [TextContent { text = \"How is the weather in New York today?\" }] }, AiMessage { text = null toolExecutionRequests = [ToolExecutionRequest { id = null, name = \"execQuery\", arguments = \"{\n",
      "  \"query\" : \"weather in New York today\"\n",
      "}\" }] }, ToolExecutionResultMessage { id = null toolName = \"execQuery\" text = \"Cold, with a low of 13 degrees\" }]}, chunk=!} \n",
      "ToolExecutionRequest id is null! \n",
      "ToolExecutionResultMessage id is null! \n",
      "StreamingOutput: NodeOutput{node=agent, state={messages=[UserMessage { name = null contents = [TextContent { text = \"How is the weather in New York today?\" }] }, AiMessage { text = null toolExecutionRequests = [ToolExecutionRequest { id = null, name = \"execQuery\", arguments = \"{\n",
      "  \"query\" : \"weather in New York today\"\n",
      "}\" }] }, ToolExecutionResultMessage { id = null toolName = \"execQuery\" text = \"Cold, with a low of 13 degrees\" }, AiMessage { text = \"The current weather in New York is cold, with the temperature expected to drop to 13 degrees. Please dress accordingly!\" toolExecutionRequests = [] }]}} \n",
      "StreamingOutput: NodeOutput{node=__END__, state={messages=[UserMessage { name = null contents = [TextContent { text = \"How is the weather in New York today?\" }] }, AiMessage { text = null toolExecutionRequests = [ToolExecutionRequest { id = null, name = \"execQuery\", arguments = \"{\n",
      "  \"query\" : \"weather in New York today\"\n",
      "}\" }] }, ToolExecutionResultMessage { id = null toolName = \"execQuery\" text = \"Cold, with a low of 13 degrees\" }, AiMessage { text = \"The current weather in New York is cold, with the temperature expected to drop to 13 degrees. Please dress accordingly!\" toolExecutionRequests = [] }]}} \n"
     ]
    }
   ],
   "source": [
    "testToolsStreamingChat(\"qwen2.5:7b\");"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {},
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "START \n",
      "ToolExecutionRequest id is null! \n",
      "StreamingOutput: NodeOutput{node=__START__, state={messages=[UserMessage { name = null contents = [TextContent { text = \"How is the weather in New York today?\" }] }]}} \n",
      "ToolExecutionRequest id is null! \n",
      "execute: execQuery \n",
      "ToolExecutionRequest id is null! \n",
      "ToolExecutionResultMessage id is null! \n",
      "StreamingOutput: NodeOutput{node=agent, state={messages=[UserMessage { name = null contents = [TextContent { text = \"How is the weather in New York today?\" }] }, AiMessage { text = null toolExecutionRequests = [ToolExecutionRequest { id = null, name = \"execQuery\", arguments = \"{\n",
      "  \"query\" : \"New York weather today\"\n",
      "}\" }] }]}} \n",
      "ToolExecutionRequest id is null! \n",
      "ToolExecutionResultMessage id is null! \n",
      "StreamingOutput: NodeOutput{node=tools, state={messages=[UserMessage { name = null contents = [TextContent { text = \"How is the weather in New York today?\" }] }, AiMessage { text = null toolExecutionRequests = [ToolExecutionRequest { id = null, name = \"execQuery\", arguments = \"{\n",
      "  \"query\" : \"New York weather today\"\n",
      "}\" }] }, ToolExecutionResultMessage { id = null toolName = \"execQuery\" text = \"Cold, with a low of 13 degrees\" }]}} \n",
      "StreamingOutput: StreamingOutput{node=agent, state={messages=[UserMessage { name = null contents = [TextContent { text = \"How is the weather in New York today?\" }] }, AiMessage { text = null toolExecutionRequests = [ToolExecutionRequest { id = null, name = \"execQuery\", arguments = \"{\n",
      "  \"query\" : \"New York weather today\"\n",
      "}\" }] }, ToolExecutionResultMessage { id = null toolName = \"execQuery\" text = \"Cold, with a low of 13 degrees\" }]}, chunk=I} \n",
      "StreamingOutput: StreamingOutput{node=agent, state={messages=[UserMessage { name = null contents = [TextContent { text = \"How is the weather in New York today?\" }] }, AiMessage { text = null toolExecutionRequests = [ToolExecutionRequest { id = null, name = \"execQuery\", arguments = \"{\n",
      "  \"query\" : \"New York weather today\"\n",
      "}\" }] }, ToolExecutionResultMessage { id = null toolName = \"execQuery\" text = \"Cold, with a low of 13 degrees\" }]}, chunk='ve} \n",
      "StreamingOutput: StreamingOutput{node=agent, state={messages=[UserMessage { name = null contents = [TextContent { text = \"How is the weather in New York today?\" }] }, AiMessage { text = null toolExecutionRequests = [ToolExecutionRequest { id = null, name = \"execQuery\", arguments = \"{\n",
      "  \"query\" : \"New York weather today\"\n",
      "}\" }] }, ToolExecutionResultMessage { id = null toolName = \"execQuery\" text = \"Cold, with a low of 13 degrees\" }]}, chunk= called} \n",
      "StreamingOutput: StreamingOutput{node=agent, state={messages=[UserMessage { name = null contents = [TextContent { text = \"How is the weather in New York today?\" }] }, AiMessage { text = null toolExecutionRequests = [ToolExecutionRequest { id = null, name = \"execQuery\", arguments = \"{\n",
      "  \"query\" : \"New York weather today\"\n",
      "}\" }] }, ToolExecutionResultMessage { id = null toolName = \"execQuery\" text = \"Cold, with a low of 13 degrees\" }]}, chunk= the} \n",
      "StreamingOutput: StreamingOutput{node=agent, state={messages=[UserMessage { name = null contents = [TextContent { text = \"How is the weather in New York today?\" }] }, AiMessage { text = null toolExecutionRequests = [ToolExecutionRequest { id = null, name = \"execQuery\", arguments = \"{\n",
      "  \"query\" : \"New York weather today\"\n",
      "}\" }] }, ToolExecutionResultMessage { id = null toolName = \"execQuery\" text = \"Cold, with a low of 13 degrees\" }]}, chunk= tool} \n",
      "StreamingOutput: StreamingOutput{node=agent, state={messages=[UserMessage { name = null contents = [TextContent { text = \"How is the weather in New York today?\" }] }, AiMessage { text = null toolExecutionRequests = [ToolExecutionRequest { id = null, name = \"execQuery\", arguments = \"{\n",
      "  \"query\" : \"New York weather today\"\n",
      "}\" }] }, ToolExecutionResultMessage { id = null toolName = \"execQuery\" text = \"Cold, with a low of 13 degrees\" }]}, chunk= and} \n",
      "StreamingOutput: StreamingOutput{node=agent, state={messages=[UserMessage { name = null contents = [TextContent { text = \"How is the weather in New York today?\" }] }, AiMessage { text = null toolExecutionRequests = [ToolExecutionRequest { id = null, name = \"execQuery\", arguments = \"{\n",
      "  \"query\" : \"New York weather today\"\n",
      "}\" }] }, ToolExecutionResultMessage { id = null toolName = \"execQuery\" text = \"Cold, with a low of 13 degrees\" }]}, chunk= it} \n",
      "StreamingOutput: StreamingOutput{node=agent, state={messages=[UserMessage { name = null contents = [TextContent { text = \"How is the weather in New York today?\" }] }, AiMessage { text = null toolExecutionRequests = [ToolExecutionRequest { id = null, name = \"execQuery\", arguments = \"{\n",
      "  \"query\" : \"New York weather today\"\n",
      "}\" }] }, ToolExecutionResultMessage { id = null toolName = \"execQuery\" text = \"Cold, with a low of 13 degrees\" }]}, chunk= appears} \n",
      "StreamingOutput: StreamingOutput{node=agent, state={messages=[UserMessage { name = null contents = [TextContent { text = \"How is the weather in New York today?\" }] }, AiMessage { text = null toolExecutionRequests = [ToolExecutionRequest { id = null, name = \"execQuery\", arguments = \"{\n",
      "  \"query\" : \"New York weather today\"\n",
      "}\" }] }, ToolExecutionResultMessage { id = null toolName = \"execQuery\" text = \"Cold, with a low of 13 degrees\" }]}, chunk= that} \n",
      "StreamingOutput: StreamingOutput{node=agent, state={messages=[UserMessage { name = null contents = [TextContent { text = \"How is the weather in New York today?\" }] }, AiMessage { text = null toolExecutionRequests = [ToolExecutionRequest { id = null, name = \"execQuery\", arguments = \"{\n",
      "  \"query\" : \"New York weather today\"\n",
      "}\" }] }, ToolExecutionResultMessage { id = null toolName = \"execQuery\" text = \"Cold, with a low of 13 degrees\" }]}, chunk= the} \n",
      "StreamingOutput: StreamingOutput{node=agent, state={messages=[UserMessage { name = null contents = [TextContent { text = \"How is the weather in New York today?\" }] }, AiMessage { text = null toolExecutionRequests = [ToolExecutionRequest { id = null, name = \"execQuery\", arguments = \"{\n",
      "  \"query\" : \"New York weather today\"\n",
      "}\" }] }, ToolExecutionResultMessage { id = null toolName = \"execQuery\" text = \"Cold, with a low of 13 degrees\" }]}, chunk= current} \n",
      "StreamingOutput: StreamingOutput{node=agent, state={messages=[UserMessage { name = null contents = [TextContent { text = \"How is the weather in New York today?\" }] }, AiMessage { text = null toolExecutionRequests = [ToolExecutionRequest { id = null, name = \"execQuery\", arguments = \"{\n",
      "  \"query\" : \"New York weather today\"\n",
      "}\" }] }, ToolExecutionResultMessage { id = null toolName = \"execQuery\" text = \"Cold, with a low of 13 degrees\" }]}, chunk= weather} \n",
      "StreamingOutput: StreamingOutput{node=agent, state={messages=[UserMessage { name = null contents = [TextContent { text = \"How is the weather in New York today?\" }] }, AiMessage { text = null toolExecutionRequests = [ToolExecutionRequest { id = null, name = \"execQuery\", arguments = \"{\n",
      "  \"query\" : \"New York weather today\"\n",
      "}\" }] }, ToolExecutionResultMessage { id = null toolName = \"execQuery\" text = \"Cold, with a low of 13 degrees\" }]}, chunk= in} \n",
      "StreamingOutput: StreamingOutput{node=agent, state={messages=[UserMessage { name = null contents = [TextContent { text = \"How is the weather in New York today?\" }] }, AiMessage { text = null toolExecutionRequests = [ToolExecutionRequest { id = null, name = \"execQuery\", arguments = \"{\n",
      "  \"query\" : \"New York weather today\"\n",
      "}\" }] }, ToolExecutionResultMessage { id = null toolName = \"execQuery\" text = \"Cold, with a low of 13 degrees\" }]}, chunk= New} \n",
      "StreamingOutput: StreamingOutput{node=agent, state={messages=[UserMessage { name = null contents = [TextContent { text = \"How is the weather in New York today?\" }] }, AiMessage { text = null toolExecutionRequests = [ToolExecutionRequest { id = null, name = \"execQuery\", arguments = \"{\n",
      "  \"query\" : \"New York weather today\"\n",
      "}\" }] }, ToolExecutionResultMessage { id = null toolName = \"execQuery\" text = \"Cold, with a low of 13 degrees\" }]}, chunk= York} \n",
      "StreamingOutput: StreamingOutput{node=agent, state={messages=[UserMessage { name = null contents = [TextContent { text = \"How is the weather in New York today?\" }] }, AiMessage { text = null toolExecutionRequests = [ToolExecutionRequest { id = null, name = \"execQuery\", arguments = \"{\n",
      "  \"query\" : \"New York weather today\"\n",
      "}\" }] }, ToolExecutionResultMessage { id = null toolName = \"execQuery\" text = \"Cold, with a low of 13 degrees\" }]}, chunk= is} \n",
      "StreamingOutput: StreamingOutput{node=agent, state={messages=[UserMessage { name = null contents = [TextContent { text = \"How is the weather in New York today?\" }] }, AiMessage { text = null toolExecutionRequests = [ToolExecutionRequest { id = null, name = \"execQuery\", arguments = \"{\n",
      "  \"query\" : \"New York weather today\"\n",
      "}\" }] }, ToolExecutionResultMessage { id = null toolName = \"execQuery\" text = \"Cold, with a low of 13 degrees\" }]}, chunk= cold} \n",
      "StreamingOutput: StreamingOutput{node=agent, state={messages=[UserMessage { name = null contents = [TextContent { text = \"How is the weather in New York today?\" }] }, AiMessage { text = null toolExecutionRequests = [ToolExecutionRequest { id = null, name = \"execQuery\", arguments = \"{\n",
      "  \"query\" : \"New York weather today\"\n",
      "}\" }] }, ToolExecutionResultMessage { id = null toolName = \"execQuery\" text = \"Cold, with a low of 13 degrees\" }]}, chunk=,} \n",
      "StreamingOutput: StreamingOutput{node=agent, state={messages=[UserMessage { name = null contents = [TextContent { text = \"How is the weather in New York today?\" }] }, AiMessage { text = null toolExecutionRequests = [ToolExecutionRequest { id = null, name = \"execQuery\", arguments = \"{\n",
      "  \"query\" : \"New York weather today\"\n",
      "}\" }] }, ToolExecutionResultMessage { id = null toolName = \"execQuery\" text = \"Cold, with a low of 13 degrees\" }]}, chunk= with} \n",
      "StreamingOutput: StreamingOutput{node=agent, state={messages=[UserMessage { name = null contents = [TextContent { text = \"How is the weather in New York today?\" }] }, AiMessage { text = null toolExecutionRequests = [ToolExecutionRequest { id = null, name = \"execQuery\", arguments = \"{\n",
      "  \"query\" : \"New York weather today\"\n",
      "}\" }] }, ToolExecutionResultMessage { id = null toolName = \"execQuery\" text = \"Cold, with a low of 13 degrees\" }]}, chunk= a} \n",
      "StreamingOutput: StreamingOutput{node=agent, state={messages=[UserMessage { name = null contents = [TextContent { text = \"How is the weather in New York today?\" }] }, AiMessage { text = null toolExecutionRequests = [ToolExecutionRequest { id = null, name = \"execQuery\", arguments = \"{\n",
      "  \"query\" : \"New York weather today\"\n",
      "}\" }] }, ToolExecutionResultMessage { id = null toolName = \"execQuery\" text = \"Cold, with a low of 13 degrees\" }]}, chunk= low} \n",
      "StreamingOutput: StreamingOutput{node=agent, state={messages=[UserMessage { name = null contents = [TextContent { text = \"How is the weather in New York today?\" }] }, AiMessage { text = null toolExecutionRequests = [ToolExecutionRequest { id = null, name = \"execQuery\", arguments = \"{\n",
      "  \"query\" : \"New York weather today\"\n",
      "}\" }] }, ToolExecutionResultMessage { id = null toolName = \"execQuery\" text = \"Cold, with a low of 13 degrees\" }]}, chunk= temperature} \n",
      "StreamingOutput: StreamingOutput{node=agent, state={messages=[UserMessage { name = null contents = [TextContent { text = \"How is the weather in New York today?\" }] }, AiMessage { text = null toolExecutionRequests = [ToolExecutionRequest { id = null, name = \"execQuery\", arguments = \"{\n",
      "  \"query\" : \"New York weather today\"\n",
      "}\" }] }, ToolExecutionResultMessage { id = null toolName = \"execQuery\" text = \"Cold, with a low of 13 degrees\" }]}, chunk= of} \n",
      "StreamingOutput: StreamingOutput{node=agent, state={messages=[UserMessage { name = null contents = [TextContent { text = \"How is the weather in New York today?\" }] }, AiMessage { text = null toolExecutionRequests = [ToolExecutionRequest { id = null, name = \"execQuery\", arguments = \"{\n",
      "  \"query\" : \"New York weather today\"\n",
      "}\" }] }, ToolExecutionResultMessage { id = null toolName = \"execQuery\" text = \"Cold, with a low of 13 degrees\" }]}, chunk= 13} \n",
      "StreamingOutput: StreamingOutput{node=agent, state={messages=[UserMessage { name = null contents = [TextContent { text = \"How is the weather in New York today?\" }] }, AiMessage { text = null toolExecutionRequests = [ToolExecutionRequest { id = null, name = \"execQuery\", arguments = \"{\n",
      "  \"query\" : \"New York weather today\"\n",
      "}\" }] }, ToolExecutionResultMessage { id = null toolName = \"execQuery\" text = \"Cold, with a low of 13 degrees\" }]}, chunk= degrees} \n",
      "ToolExecutionRequest id is null! \n",
      "ToolExecutionResultMessage id is null! \n",
      "StreamingOutput: StreamingOutput{node=agent, state={messages=[UserMessage { name = null contents = [TextContent { text = \"How is the weather in New York today?\" }] }, AiMessage { text = null toolExecutionRequests = [ToolExecutionRequest { id = null, name = \"execQuery\", arguments = \"{\n",
      "  \"query\" : \"New York weather today\"\n",
      "}\" }] }, ToolExecutionResultMessage { id = null toolName = \"execQuery\" text = \"Cold, with a low of 13 degrees\" }]}, chunk=.} \n",
      "ToolExecutionRequest id is null! \n",
      "ToolExecutionResultMessage id is null! \n",
      "StreamingOutput: NodeOutput{node=agent, state={messages=[UserMessage { name = null contents = [TextContent { text = \"How is the weather in New York today?\" }] }, AiMessage { text = null toolExecutionRequests = [ToolExecutionRequest { id = null, name = \"execQuery\", arguments = \"{\n",
      "  \"query\" : \"New York weather today\"\n",
      "}\" }] }, ToolExecutionResultMessage { id = null toolName = \"execQuery\" text = \"Cold, with a low of 13 degrees\" }, AiMessage { text = \"I've called the tool and it appears that the current weather in New York is cold, with a low temperature of 13 degrees.\" toolExecutionRequests = [] }]}} \n",
      "StreamingOutput: NodeOutput{node=__END__, state={messages=[UserMessage { name = null contents = [TextContent { text = \"How is the weather in New York today?\" }] }, AiMessage { text = null toolExecutionRequests = [ToolExecutionRequest { id = null, name = \"execQuery\", arguments = \"{\n",
      "  \"query\" : \"New York weather today\"\n",
      "}\" }] }, ToolExecutionResultMessage { id = null toolName = \"execQuery\" text = \"Cold, with a low of 13 degrees\" }, AiMessage { text = \"I've called the tool and it appears that the current weather in New York is cold, with a low temperature of 13 degrees.\" toolExecutionRequests = [] }]}} \n"
     ]
    }
   ],
   "source": [
    "\n",
    "testToolsStreamingChat(\"llama3.1:latest\");"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Java (rjk 2.2.0)",
   "language": "java",
   "name": "rapaio-jupyter-kernel"
  },
  "language_info": {
   "codemirror_mode": "java",
   "file_extension": ".jshell",
   "mimetype": "text/x-java-source",
   "name": "java",
   "nbconvert_exporter": "script",
   "pygments_lexer": "java",
   "version": "22.0.2+9-70"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
